home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StyleXlater.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  329 lines

  1. /*
  2.  * @(#)StyleXlater.java    1.12 98/03/13
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text.html;
  21.  
  22. import java.awt.Color;
  23. import java.awt.Component;
  24. import java.awt.Font;
  25. import java.awt.FontMetrics;
  26. import java.util.Hashtable;
  27. import java.util.StringTokenizer;
  28. import com.sun.java.swing.*;
  29. import com.sun.java.swing.text.*;
  30.  
  31.  
  32. /**
  33.  * A class to help translate cascading style definitions into 
  34.  * paragraph & character styles.
  35.  *
  36.  * @author  Jill Nakata
  37.  * @version 1.12 03/13/98
  38.  */
  39. class StyleXlater {
  40.  
  41.     //
  42.     // Property Names in Cascading Style Sheets
  43.     //
  44.     public static final String BACKGROUND_COLOR = "background-color";
  45.     public static final String BORDER_TOP = "border-top";
  46.     public static final String BORDER_BOTTOM = "border-bottom";
  47.     public static final String BORDER_LEFT = "border-left";
  48.     public static final String BORDER_RIGHT = "border-right";
  49.     public static final String COLOR = "color";
  50.     public static final String FONT_FAMILY = "font-family";
  51.     public static final String FONT_SIZE = "font-size";
  52.     public static final String FONT_SIZES = "font-sizes";
  53.     public static final String FONT_STYLE = "font-style";
  54.     public static final String FONT_WEIGHT = "font-weight";
  55.     public static final String LIST_STYLE_IMAGE = "list-style-image";
  56.     public static final String LIST_STYLE_TYPE = "list-style-type";
  57.     public static final String MARGIN_TOP = "margin-top";
  58.     public static final String MARGIN_BOTTOM = "margin-bottom";
  59.     public static final String MARGIN_LEFT = "margin-left";
  60.     public static final String MARGIN_RIGHT = "margin-right";
  61.     public static final String TEXT_DECORATION = "text-decoration";
  62.     public static final String TEXT_ALIGN = "text-align";
  63.     public static final String VERTICAL_ALIGN = "vertical-align";
  64.  
  65.     /**
  66.      * Construct a Style Translator
  67.      */
  68.     public StyleXlater()
  69.     {
  70.     }
  71.  
  72.      /**
  73.       * Translate the parser attribute name & value into a paragraph or
  74.       * character style name & value.
  75.       */
  76.      public static void translateProperty(StyleSheet ss, Style s, 
  77.                        String name, String value) {
  78.  
  79.     //
  80.         // Remove begin and end quotes of the value "xxx" => xxx.
  81.     //
  82.     String val = Utilities.removeSurroundingQuotes(value);
  83.  
  84.     if (name.equalsIgnoreCase(FONT_FAMILY)) {
  85.         if (val.equalsIgnoreCase("monospace") ||
  86.             val.equalsIgnoreCase("monospaced") ||
  87.             val.equalsIgnoreCase("typewriter"))
  88.           StyleConstants.setFontFamily(s, "Monospaced");
  89.         else
  90.           StyleConstants.setFontFamily(s, val);
  91.     }
  92.     else if (name.equalsIgnoreCase(FONT_SIZE)) {
  93.         setFontSize(val, s, ss);
  94.     }
  95.     else if (name.equalsIgnoreCase(FONT_WEIGHT)) {
  96.         setFontStyle(val, s);
  97.     }
  98.     else if (name.equalsIgnoreCase(FONT_STYLE)) {
  99.         setFontStyle(val, s);
  100.     }
  101.     else if (name.equalsIgnoreCase(MARGIN_TOP)) {
  102.         StyleConstants.setSpaceAbove(s, Integer.valueOf(val).intValue());
  103.     }
  104.     else if (name.equalsIgnoreCase(MARGIN_BOTTOM)) {
  105.         StyleConstants.setSpaceBelow(s, Integer.valueOf(val).intValue());
  106.     }
  107.     else if (name.equalsIgnoreCase(MARGIN_LEFT)) {
  108.         StyleConstants.setLeftIndent(s, Integer.valueOf(val).intValue());
  109.     }
  110.     else if (name.equalsIgnoreCase(MARGIN_RIGHT)) {
  111.         StyleConstants.setRightIndent(s, Integer.valueOf(val).intValue());
  112.     }
  113.     else if (name.equalsIgnoreCase(TEXT_DECORATION)) {
  114.         setTextDecoration(val, s);
  115.     }
  116.     else if (name.equalsIgnoreCase(TEXT_ALIGN)) {
  117.         setTextAlignment(val, s);
  118.     }
  119.     else if (name.equalsIgnoreCase(COLOR)) {
  120.         setForegroundColor(val, s);
  121.     }
  122.     else if (name.equalsIgnoreCase(LIST_STYLE_TYPE) ||
  123.             name.equalsIgnoreCase(LIST_STYLE_IMAGE)) {
  124.         s.addAttribute(name.toLowerCase(), val);
  125.     }
  126.     else if (name.equalsIgnoreCase(FONT_SIZES)) {
  127.         ss.setFontSizes(val);
  128.     }
  129.     else {
  130.         s.addAttribute(name.toLowerCase(), val);
  131.         }
  132.  
  133.      }
  134.  
  135.      /**
  136.       * Convert this font-size to a value between -3..3
  137.       *         xxs   xs   sm   med  lg   xlg  xxlg
  138.       *        "-3", "-2", "-1", "0", "1", "2", "3"
  139.       */
  140.     static String convertFontSizeString(String val) {
  141.  
  142.     if (val.equalsIgnoreCase("xx-small") ||
  143.         val.equalsIgnoreCase("xxsmall"))
  144.       return "-3";
  145.     else if (val.equalsIgnoreCase("x-small") ||
  146.              val.equalsIgnoreCase("xsmall"))
  147.       return "-2";
  148.     else if (val.equalsIgnoreCase("small")) 
  149.       return "-1";
  150.     else if (val.equalsIgnoreCase("medium"))
  151.       return "+0";
  152.     else if (val.equalsIgnoreCase("large"))
  153.       return "+1";
  154.     else if (val.equalsIgnoreCase("x-large") ||
  155.              val.equalsIgnoreCase("xlarge"))
  156.       return "+2";
  157.     else if (val.equalsIgnoreCase("xx-large") ||
  158.              val.equalsIgnoreCase("xxlarge"))
  159.       return "+3";
  160.     else {
  161.       System.out.println("StyleXlater.convertFontSizeString - " + 
  162.                  "Warning!  Unimplemented font size value: " + val);
  163.       return "0";
  164.     }
  165.     }
  166.  
  167.     /**
  168.      * Set the font style in the Style.
  169.      */
  170.     static void setFontStyle(String fontStyle, Style s) {
  171.  
  172.     if (fontStyle == null)
  173.       return;
  174.  
  175.     if (fontStyle.equalsIgnoreCase("bold") || fontStyle.equals("bolder")) {
  176.       StyleConstants.setBold(s, true);
  177.         }
  178.     else if (fontStyle.equalsIgnoreCase("italic")) {
  179.       StyleConstants.setItalic(s, true);
  180.         }
  181.     else if (fontStyle.equalsIgnoreCase("typewriter") ||
  182.          fontStyle.equalsIgnoreCase("monospace") ||
  183.          fontStyle.equalsIgnoreCase("monospaced")) {
  184.       StyleConstants.setFontFamily(s, "Monospaced");
  185.         }
  186.     else if (fontStyle.equalsIgnoreCase("bolditalic")) {
  187.       StyleConstants.setBold(s, true);
  188.       StyleConstants.setItalic(s, true);
  189.         }
  190.     else if (fontStyle.equalsIgnoreCase("normal")) {
  191.       ;  // plain, do nothing
  192.     }
  193.     // We do not have "light" fonts so use normal (do nothing).
  194.     else if (fontStyle.equalsIgnoreCase("light") || fontStyle.equals("lighter") ||
  195.          fontStyle.equalsIgnoreCase("oblique")) {
  196.     }
  197.     }
  198.  
  199.     /**
  200.      * Set the text decoration in the style, if supported.
  201.      */
  202.     static void setTextDecoration(String decor, Style s) {
  203.  
  204.     if (decor.equalsIgnoreCase("underline"))
  205.       StyleConstants.setUnderline(s, true);
  206. /*
  207.     else 
  208.       System.out.println("HTMXlater.translateProperty:  " + 
  209.                  "Unsupported property value: " + decor);
  210. */
  211.     }
  212.  
  213.     /**
  214.      * Set the text alignment in the style.
  215.      */
  216.     static void setTextAlignment(String align, Style s) {
  217.  
  218.     if (align.equalsIgnoreCase("left"))
  219.       StyleConstants.setAlignment(s, StyleConstants.ALIGN_LEFT);
  220.     else if (align.equalsIgnoreCase("center"))
  221.       StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
  222.     else if (align.equalsIgnoreCase("right"))
  223.       StyleConstants.setAlignment(s, StyleConstants.ALIGN_RIGHT);
  224.     else if (align.equalsIgnoreCase("justify"))
  225.       StyleConstants.setAlignment(s, StyleConstants.ALIGN_JUSTIFIED);
  226.     else 
  227.       System.out.println("HTMXlater.translateProperty:  " + 
  228.                  "Unsupported property value: " + align);
  229.     }
  230.  
  231.     /**
  232.      * Translate the property value for "color".
  233.      */
  234.     static void setForegroundColor(String val, Style s) {
  235.  
  236.         Color color;
  237.  
  238.         // e.g., "#ff7bd4"
  239.         if (val.startsWith("#")) {
  240.         color = Utilities.stringToColor(val);
  241.         StyleConstants.setForeground(s, color);
  242.     }
  243.  
  244.     // "rgb(255, 125, 213)" or "rgb(100%, 49%, 84%)"
  245.     else if (val.startsWith("rgb") || val.startsWith("RGB")) {
  246.         int index =  val.indexOf("(");
  247.         StringTokenizer st = new StringTokenizer(val.substring
  248.                     (index+1, val.length()-1), ", \t \n");
  249.         int n = 0;
  250.         int rgb[] = new int[3];
  251.         while (n < rgb.length && st.hasMoreTokens()) {
  252.         String str = st.nextToken();
  253.         if (str.endsWith("%"))
  254.           rgb[n] = (Integer.valueOf(str.substring(0, str.length()-1)).intValue() * 255) / 100;
  255.             else
  256.           rgb[n] = Integer.valueOf(str).intValue();
  257.         n++;
  258.             }
  259.         // Make sure we got three values for R, G, B.
  260.         if (n == 3) {
  261.             color = new Color(rgb[0], rgb[1], rgb[2]);
  262.             StyleConstants.setForeground(s, color);
  263.         }
  264.     }
  265.  
  266.     // "red", "mauve"
  267.     else {
  268.         color = Utilities.stringToColor(val);
  269.         StyleConstants.setForeground(s, color);
  270.     }
  271.     }
  272.  
  273.     /**
  274.      * Translate the property value for "font-size".
  275.      */
  276.     static void setFontSize(String val, Style s, StyleSheet ss) {
  277.  
  278.         int size = 0;
  279.  
  280.         // If countTokens() == 0, then this value is all numeric so
  281.         // just assume this number is setting pt size.
  282.     //
  283.         StringTokenizer st = new StringTokenizer(val, "0123456789");
  284.         if (st.countTokens() == 0) {
  285.         size = Integer.valueOf(val).intValue();;
  286.         StyleConstants.setFontSize(s, size);
  287.     }
  288.  
  289.     // "+2pt" or "-1pt"
  290.     else if ((val.startsWith("+") || val.startsWith("-")) &&
  291.         val.endsWith("pt")) {
  292.         System.out.println("StyleXlater.translateAttribute: " +
  293.         "Unimplemented font-size value " + val);
  294.     }
  295.  
  296.     // e.g., "12pt" or "14pt"
  297.     else if (val.toLowerCase().endsWith("pt")) {
  298.         size = Integer.valueOf(val.substring(0, val.length()-2)).intValue();
  299.         StyleConstants.setFontSize(s, size);
  300.     }
  301.  
  302.     // "xx-small", "x-small", "small", "medium", "large", "x-large"
  303.     else {
  304.         //
  305.         // Convert this font-size to a value between -3..3
  306.         //        xxs   xs   sm   med  lg   xlg  xxlg
  307.         // e.g., "-3" "-2", "-1", "0", "1", "2", "3"
  308.         String fontval = convertFontSizeString(val);
  309.         size = ss.getPtSize(fontval);
  310.         StyleConstants.setFontSize(s, size);
  311.     }
  312.  
  313.     //
  314.     // If is it the default style, then adjust the base
  315.     // font size in the Style Sheet so that we know
  316.     // what "medium" size maps to.
  317.     //
  318.     String styleName = (String)s.getAttribute(AttributeSet.NameAttribute);
  319.     if (size != 0 && styleName != null &&
  320.          (styleName.equals(Constants.BODY) || 
  321.          styleName.equals(StyleContext.DEFAULT_STYLE))) {
  322.         ss.setPtSize(size);
  323.     }
  324.     }
  325.  
  326. }
  327.  
  328.  
  329.